package com.example.controller;

import com.example.common.Result;
import com.example.entity.Ordersetting;
import com.example.service.OrdersettingService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/ordersetting")
public class OrdersettingController {
    @Resource
    private OrdersettingService ordersettingService;

    @RequestMapping("/selectByOrdersDate")  // /api/ordersetting/selectByOrdersDate
    public Result updateByOrders(@RequestBody Ordersetting ordersettings) {

        Ordersetting ordersetting1 = ordersettingService.orderByDate(ordersettings.getOrderDate());
        if (ordersetting1==null){
            Ordersetting ordersetting = new Ordersetting();
            ordersetting.setOrderDate(ordersettings.getOrderDate());
            ordersetting.setReservations(ordersettings.getReservations());
            ordersetting.setNumber(100- ordersettings.getReservations());
            return  Result.success( ordersettingService.saveOrUpdate(ordersetting));
        }
        if (ordersetting1.getNumber() > 0 && ordersetting1.getNumber() >=ordersettings.getReservations()) {
            Ordersetting ordersetting = new Ordersetting();
            ordersetting.setOrderDate(ordersettings.getOrderDate());
            ordersetting.setReservations(ordersetting1.getReservations() + ordersettings.getReservations());
            ordersetting.setNumber(ordersetting1.getNumber() - ordersettings.getReservations());
            return Result.success(ordersettingService.updateOrderByDate(ordersetting));
        }


        return Result.error("5000","超出预约人数");
    }


    @RequestMapping("/updateByOrders")  // /api/ordersetting/selectByOrdersDate
    public Result updateByOrdersNumber(@RequestBody Ordersetting ordersettings) {

     Ordersetting ordersetting1 = ordersettingService.orderByDate(ordersettings.getOrderDate());
        if (ordersetting1==null){

            return  Result.success( ordersettingService.save(ordersettings));
        }
            Ordersetting ordersetting = new Ordersetting();
            ordersetting.setOrderDate(ordersettings.getOrderDate());
            ordersetting.setNumber(ordersettings.getNumber());
            return Result.success(ordersettingService.updateOrderByDate(ordersetting));
    }


    @RequestMapping("/enableOrdersetting") // /api/ordersetting/enableOrdersetting
    public Result enableOrdersetting() {
        List<Ordersetting> ordersettings = ordersettingService.enableOrdersetting();
        return Result.success(ordersettings);
    }

    @PostMapping
    public Result<?> save(@RequestBody Ordersetting ordersetting) {
        return Result.success(ordersettingService.save(ordersetting));
    }

    @PutMapping
    public Result<?> update(@RequestBody Ordersetting ordersetting) {
        return Result.success(ordersettingService.updateById(ordersetting));
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        ordersettingService.removeById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Ordersetting> findById(@PathVariable Long id) {
        return Result.success(ordersettingService.getById(id));
    }

    @GetMapping
    public Result<List<Ordersetting>> findAll() {
        return Result.success(ordersettingService.list());
    }

    @GetMapping("/page")
    public Result<IPage<Ordersetting>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                                @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(ordersettingService.page(new Page<>(pageNum, pageSize), Wrappers.<Ordersetting>lambdaQuery().like(Ordersetting::getName, name)));
    }

}